﻿<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
	content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<![endif]-->
<link href="../css/formcheck.css" rel="stylesheet" type="text/css" />
<link rel="import" href="../includes/baseInclude.html">
<!--[if IE 6]>
<script type="text/javascript" src="http://lib.h-ui.net/DD_belatedPNG_0.0.8a-min.js" ></script>
<script>DD_belatedPNG.fix('*');</script>
<![endif]-->
<title>角色管理</title>
</head>
<body ng-app="userApp" ng-controller="userCtrl" style="margin-top:16px">
	<article class="page-container">
		<form method="post" name="someForm">
			<div class="panel-primary">
				<div class="panel-body">
					<table class="table table-border table-bg table-bordered table-layout:fixed;">
						<tbody class="text-c ">
							<tr>
								<th style="width:139px;"><span class='c-red'>*</span>角色名</th>
								<td style="width:207px;">
									<div style="height: 27px;"
										 ng-init="nameVailData=
                                         [{cond:'someForm.name.$error.maxlength',msg:'至多20个字符'},
                                          {cond:'someForm.name.$error.required&&someForm.name.$dirty',msg:'sop名称不能为空'}]">
										<input  ng-model="name" type="text" class="input-text radius size-S" value="" placeholder="请输入角色名,最多20字符"
												ng-class="{error:someForm.name.$error.maxlength||someForm.name.$error.required&&someForm.name.$dirty}"
												name="name" ng-maxlength="20" maxlength="21" required>
                                            <span ng-repeat="nameMessage in nameVailData">
                                            <span class="userErrorMessage " ng-show="{{nameMessage.cond}}" ng-bind="nameMessage.msg"></span>
                                            </span>
									</div>
								</td>
								<th style="width:174px;">描述</th>
								<td style="width:207px;">
									<div style="height: 27px;"
										 ng-init="descVailData=
                                         [{cond:'someForm.desc.$error.maxlength',msg:'至多20个字符'},
                                          ]">
										<input  ng-model="desc" type="text" class="input-text radius size-S" value="" placeholder="请输入角色描述"
												ng-class="{error:someForm.desc.$error.maxlength&&someForm.desc.$dirty}"
												name="desc" ng-maxlength="20" maxlength="21">
                                            <span ng-repeat="descMessage in descVailData">
                                            <span class="userErrorMessage " ng-show="{{descMessage.cond}}" ng-bind="descMessage.msg"></span>
                                            </span>
									</div>
								</td> 
							</tr>
						</tbody>
					</table>
				</div>
			</div>
			<div class=" text-c" style="text-align: center;margin-bottom: 30px;">
				<input class="btn btn-primary radius" type="button" ng-disabled="someForm.$invalid"
					   ng-click="addUserSubmit()" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
			</div>
		</form>
	</article>
    <script type="text/javascript" src="../lib/layer/2.4/layer.js"></script>
	<script type="text/javascript" src="../js/laydate/laydate.js"></script>
    <script type="text/javascript" src="../lib/jquery/jquery.params.js"></script>
    <script type="text/javascript" src="../js/instruct.js"></script>
<script>
	//angularJs post 提交时HTTP头设置
	var transform = function(data) {
		return $.param(data);
	}, postCfg = {
		headers : {
			'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'
		},
		transformRequest : transform
	};
	var app = angular.module('userApp', []);
	app.directive('onFinishRenderFilters', function($timeout) {
		return {
			restrict : 'A',
			link : function(scope, element, attr) {
				if (scope.$last === true) {
					$timeout(function() {
						scope.$emit('ngRepeatFinished');
					});
				}
			}
		};
	});
	app.controller('userCtrl', function($scope, $http) {
		var contractId = $.query.get("editId");
		$scope.infoContractId = contractId;
		$scope.editId=contractId;
		/*添加，修改*/
		$scope.getTypeVlue = function(title,grup){
			$scope.contract.industry = grup;
		};
		if($scope.infoContractId == null || $scope.infoContractId ==""){

		}else{
			var data = {
				'id':$scope.infoContractId
			};
			$http.post('/role/getRole',data,postCfg)
					.success(function(resp){
						$scope.name=resp.name;
						$scope.desc=resp.description;
					});

		}

		$scope.addUserSubmit = function(){
			if($scope.infoContractId == null || $scope.infoContractId ==""){
				$scope.addData = {
					'name':$scope.name,
					'description':$scope.desc
				};
			}else {
				$scope.addData = {
					'id':$scope.infoContractId,
					'name':$scope.name,
					'description':$scope.desc
				};
			}
			if($scope.infoContractId == null || $scope.infoContractId ==""){
				$http.post('/role/add',$scope.addData,postCfg)
						.success(function(resp){
							if(validateSuccessData(resp)){
								window.parent.layer.msg('添加成功',{icon:1,time:1000});
								window.parent.refresh();
								layer_close();
							}else{
								layer.msg(resp.errorMessage,{icon:2,time:1000});
							}
						});
			}else{
				$http.post('/role/update',addData,postCfg)
						.success(function(resp){
							if(validateSuccessData(resp)){
								window.parent.layer.msg('修改成功',{icon:1,time:1000});
								window.parent.refresh();
								layer_close();
							}else{
								layer.msg(resp.errorMessage,{icon:2,time:1000});
							}
						});
			}
		};

		/*错误信息提示*/
		function validateSuccessData(data){
			if((typeof(data) == 'string') && (data == 'true')){
				return true;
			}
			if((typeof(data) == 'boolean') && (data == true)){
				return true;
			}
			if(data.id){
				return true;
			}
			return false;
		}
	});

</script>
</body>
</html>